#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/syscall.h>

#define for_rnd(i, j, n, rnd_data) for(i = get_rnd(n, &rnd_data), j = 0; j < n; j++, i = (i == n-1 ? 0 : i+1))

int get_rnd(int range, struct drand48_data* rnd_data) {
  double res;
  drand48_r(rnd_data, &res);
	int rnd_index = res * range - 1;
	if (rnd_index > range - 1 || rnd_index < 0) {
    printf("random out of range %d:  %d, aborting...\n", range, rnd_index);
	}
	return rnd_index;
}

double get_current_time() {
  struct timespec ts;
  syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts);
  double clock_time = (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9; 
  return clock_time;
}

int main() {
  int n = 10;
  struct drand48_data rnd_data;
  srand48_r(get_current_time(), &rnd_data);
  //  for (int i = get_rnd(n, &rnd_data), j = 0; j < n; j++, i = (i == n - 1 ? 0 : i + 1)) {
  int i, j;
  for_rnd(i, j, n, rnd_data) {
    printf("%d\n", i);
  }
}
